import json

def read_file_to_dict(filename):
    data_dict = {}
    with open(filename, 'r', encoding='utf-8') as file:
        for line in file:
            # 去掉空行和空白字符
            line = line.strip()
            if line:
                # 将每行解析为字典，并更新到总字典中
                try:
                    line_dict = json.loads(line)
                    data_dict.update(line_dict)
                except json.JSONDecodeError as e:
                    print(f"Error decoding JSON from line: {line} - {e}")
    return data_dict

def compare_files(file1, file2):
    dict1 = read_file_to_dict(file1)
    dict2 = read_file_to_dict(file2)

    # 找到相同的key
    common_keys = set(dict1.keys()).intersection(set(dict2.keys()))

    # 打印相同key的内容
    all_songs = dict()
    for key in common_keys:
        song = dict(song=dict2[key], lrc = dict1[key])
        all_songs[key] = song

    print(f"-----all_songs: {json.dumps(all_songs, ensure_ascii=False, indent=2)}")

    # 使用示例
file1 = 'lrc.txt'
file2 = 'songs.txt'
compare_files(file1, file2)
